/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.OrderDao;
import com.techshop.dao.OrderDetailDao;
import com.techshop.model.Order;
import com.techshop.model.OrderDetail;
import java.util.List;
import org.hibernate.Query;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class OrderDaoImpl extends HibernateDaoSupport implements OrderDao {

    @Override
    public Order getById(int deliveryNoteId) {
        try {
            return (Order) getSession().get(Order.class, deliveryNoteId);
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Order> getAll() {
        try {
            return getSession().createQuery("FROM Order").list();
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(Order deliveryNote) {
        try {
            if (getById(deliveryNote.getId()) != null) {
                return false;
            } else {
                getHibernateTemplate().save(deliveryNote);
                return true;
            }
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean update(Order deliveryNote) {
        try {
            if (getById(deliveryNote.getId()) == null) {
                return false;
            } else {
                getHibernateTemplate().update(getHibernateTemplate().merge(deliveryNote));
                try {
                    for (Object o : deliveryNote.getOrderDetails()) {
                        OrderDetail orderDetail = (OrderDetail) o;
                        ValidatePhonePubSubClient.updatePhone(orderDetail.getPhoneCode().getId());
                    }
                } catch (Exception ex) {
                    logger.error(ex);
                }
                return true;
            }
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Order deliveryNote) {
        try {
            if (getById(deliveryNote.getId()) == null) {
                return false;
            } else {
                getHibernateTemplate().delete(getHibernateTemplate().merge(deliveryNote));
                return true;
            }
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public List<Order> search(String phone, String date, boolean state, int storeId) {
        try {
            String sql = "select * from tgdd7.Order where date(date) like :date and phone like :phone and state = :state and store_id = :storeId";
            Query query = getSession().createSQLQuery(sql).addEntity(Order.class);
            return query.setParameter("date", "%" + date + "%").setParameter("phone", "%" + phone + "%").setParameter("state", state).setParameter("storeId", storeId).list();
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Order> searchNotStore(String phone, String date, boolean state) {
        try {
            String sql = "select * from tgdd7.Order where date(date) like :date and phone like :phone and state = :state";
            Query query = getSession().createSQLQuery(sql).addEntity(Order.class);
            return query.setParameter("date", "%" + date + "%").setParameter("phone", "%" + phone + "%").setParameter("state", state).list();
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
}